<?php

namespace Admin\Controller;

use Think\Controller;

class AdminController extends CommonController
{
    public function lst()
    {
        $admin = M();
        $res = $admin->query("SELECT * FROM admin");
        $this->assign('res', $res);
//       var_dump($res);
        $this->display();
    }

    public function add()
    {
        $admin = D('admin');
//       $res = $admin->query("SELECT name,email FROM admin");
//        $post = $_POST;
        if (IS_POST) {
            $data['name'] = I('post.name');
            $data['password'] = I('post.password');
            $data['email'] = I('post.email');
            if ($admin->create($data)) {
                if ($admin->add()) {
                    $this->success('添加用户成功！', U('lst'));
                } else {
                    $this->arror('添加用户失败！');
                }
            } else {
                $this->error($admin->getError());
            }
            return;
        }
        $this->display();
    }

    public function alter()//修改
    {
        $admin = D('Admin');
        $id = I('get.id');
        $res = $admin->query("SELECT * FROM admin WHERE  id={$id}");
        $this->assign('res', $res);
//        $res=$admin->find(I('id'));
        if (IS_POST) {
            $data['name'] = I('post.name');
            $data['id'] = I('post.id');
            $data['password'] = I('post.password');
            $data['email'] = I('post.email');
            if ($admin->create($data)) {
                if ($admin->save($data)) {
                    $this->success('修改成功！', U('lst'));
                } else {
                    $this->error('修改失败！');
                }
            } else {
                $this->error($admin->getError());
            }
            return;
        }
        $this->display();
    }

    public function del()//删除
    {
        $admin = M("admin"); // 实例化User对象
        if ($admin->delete(I('id'))) {
            $this->success('删除成功！', U('lst'));
        } else {
            $this->arror('删除失败！');
        }

    }

}

